﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using VNet.VisiSuite.Common.Logger;

namespace VNet.VisiSuite.Controls
{
    public partial class TopSlidePanel : VNetUserControl
    {
        public delegate void OnClosePanel();

        private OnClosePanel _onClosePanel;
        private string _title = "";

        public TopSlidePanel()
        {
            InitializeComponent();
            _topPanel.DataContext = this;
            //DataContext = this;
        }

        public string Title
        {
            get { return _title; }
        }

        public void SetTiles(string title, IList<VNetMenuItem> menuItems)
        {
            _tiles.ItemsSource = new ArrayList();
            _tiles.Items.Refresh();

            _title = title;
            NotifyPropertyChanged("Title");

            _tiles.ItemsSource = menuItems;
            _tiles.Items.Refresh();
        }

        public void OnCloseCallback(OnClosePanel callback)
        {
            _onClosePanel = callback;
        }

        public bool Contains(double ycoord)
        {
            return (ycoord < _topPanel.ActualHeight + _topPanelTranslate.Y) ? true : false;
        }

        public void Show()
        {
            if (_tiles.Items.Count > 0)
            {
                _tiles.UnselectAll();
                var duration = new Duration(new TimeSpan(0, 0, 0, 0, 300));
                var anim = new DoubleAnimation(0, duration);
                _topPanelTranslate.BeginAnimation(TranslateTransform.YProperty, anim);
            }
        }

        public void Hide()
        {
            if (_topPanelTranslate.Y == 0)
            {
                _tiles.UnselectAll();
                double hh = _topPanel.ActualHeight;
                var duration = new Duration(new TimeSpan(0, 0, 0, 0, 150));
                var anim = new DoubleAnimation(-hh, duration);
                _topPanelTranslate.BeginAnimation(TranslateTransform.YProperty, anim);
            }
        }

        public bool IsHidden()
        {
            double hh = _topPanel.ActualHeight;
            return _topPanelTranslate.Y == -hh;
        }

        private void OnPanelSelection(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var item = (VNetMenuItem)((ListView)sender).SelectedItems[0];
                _onClosePanel();
                if (item != null)
                {
                    item.Selected();
                }
            }
            catch (Exception ex)
            {
                FileLog.Error("OnPanelSelection() - ", ex);
            }
        }
    }
}